﻿/* （转载）用于注册自定义的文件类型
 */

/*待测试*/
namespace LSLib.Registry
{
    using Microsoft.Win32;
    using System;

    public class FileTypeRegInfo
    {
        /// <summary>
        /// 目标类型文件的扩展名
        /// </summary>
        public string ExtendName;

        /// <summary>
        /// 目标文件类型说明
        /// </summary>
        public string Description;
        /// <summary>
        /// 目标类型文件关联的图标
        /// </summary>
        public string IcoPath;
        /// <summary>
        /// 打开目标类型文件的应用程序
        /// </summary>
        public string ExePath;
        public FileTypeRegInfo()
        {
        }
        public FileTypeRegInfo(string extendName)
        {
            this.ExtendName = extendName;
        }
    }

    /// <summary>
    /// FileTypeRegister 用于注册自定义的文件类型。
    /// zhuweisky 2005.08.31
    /// </summary>
    public class FileTypeRegister
    {
        /// <summary>
        /// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。
        /// </summary> 
        public static void RegisterFileType(FileTypeRegInfo regInfo)
        {
            if (FileTypeRegistered(regInfo.ExtendName)) return;

            try
            {
                string relationName = regInfo.ExtendName.Substring(1, regInfo.ExtendName.Length - 1).ToUpper() + "_FileType";
                RegistryKey fileTypeKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName);
                fileTypeKey.SetValue("", relationName);
                fileTypeKey.Close();

                RegistryKey relationKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(relationName);
                relationKey.SetValue("", regInfo.Description);
                RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon");
                iconKey.SetValue("", regInfo.IcoPath);
                RegistryKey shellKey = relationKey.CreateSubKey("Shell");
                RegistryKey openKey = shellKey.CreateSubKey("Open");
                RegistryKey commandKey = openKey.CreateSubKey("Command");
                commandKey.SetValue("", regInfo.ExePath + " %1");

                relationKey.Close();
            }
            catch (Exception ex) { throw new Exception("设置后缀名关联错误。" + ex.Message); }
        }

        /// <summary>
        /// GetFileTypeRegInfo 得到指定文件类型关联信息
        /// </summary> 
        public static FileTypeRegInfo GetFileTypeRegInfo(string extendName)
        {
            if (!FileTypeRegistered(extendName)) return null;

            try
            {
                FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName);
                string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";
                RegistryKey relationKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(relationName);
                regInfo.Description = relationKey.GetValue("").ToString();
                RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon");
                regInfo.IcoPath = iconKey.GetValue("").ToString();
                RegistryKey shellKey = relationKey.OpenSubKey("Shell");
                RegistryKey openKey = shellKey.OpenSubKey("Open");
                RegistryKey commandKey = openKey.OpenSubKey("Command");
                string temp = commandKey.GetValue("").ToString();
                regInfo.ExePath = temp.Substring(0, temp.Length - 3);

                return regInfo;
            }
            catch (Exception ex) { throw new Exception("得到后缀名关联错误。" + ex.Message); }
        }

        /// <summary>
        /// UpdateFileTypeRegInfo 更新指定文件类型关联信息
        /// </summary> 
        public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)
        {
            if (!FileTypeRegistered(regInfo.ExtendName)) return false;

            try
            {
                string extendName = regInfo.ExtendName;
                string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";
                RegistryKey relationKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(relationName, true);
                relationKey.SetValue("", regInfo.Description);
                RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon", true);
                iconKey.SetValue("", regInfo.IcoPath);
                RegistryKey shellKey = relationKey.OpenSubKey("Shell");
                RegistryKey openKey = shellKey.OpenSubKey("Open");
                RegistryKey commandKey = openKey.OpenSubKey("Command", true);
                commandKey.SetValue("", regInfo.ExePath + " %1");
                relationKey.Close();
                return true;
            }
            catch (Exception ex) { throw new Exception("更新后缀名关联错误。" + ex.Message); }
        }

        /// <summary>
        /// FileTypeRegistered 指定文件类型是否已经注册
        /// </summary> 
        public static bool FileTypeRegistered(string extendName)
        {
            RegistryKey softwareKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extendName);

            return (softwareKey != null);
        }

        /// <summary>
        /// 删除指定后缀名
        /// </summary> 
        public static void DeleteRegistered(string extendName)
        {
            Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey(extendName);
        }
    }
}
